//
//  ResponseModel.m
//  SXWallStove
//
//  Created by ZhangYeKun on 2017/2/13.
//  Copyright © 2017年 Fidetro. All rights reserved.
//

#import "ResponseModel.h"

@implementation ResponseModel

- (instancetype)initWithHexadecimalBackContent:(NSString *)backContent propertyBytes:(NSArray<NSString *> *)propertyBytes{
    
    if ([super init]) {
        NSInteger bytesCount = 0;
        for (NSString *bytes in propertyBytes) {
            
            bytesCount += [bytes integerValue];
            
        }
       
        
        NSInteger location = 0;
        NSArray *propertyNames = [self propertyOfSelf];
        
//        if (bytesCount != [backContent length]) {
//            
//            for (NSInteger i = 0; i < propertyBytes.count; i++) {
//                NSString *substring = @"";
//                for (NSInteger num = 0; num<[propertyBytes[i] integerValue]; num++) {
//                    substring = [NSString stringWithFormat:@"%@0",substring];
//                }
//                
//                [self setPropertyWithName:propertyNames[i] object:substring];
//                
//                
//                
//            }
//            return self;
//        }
        NSLog(@"%@",propertyBytes);
        
        for (NSInteger i = 0; i < propertyBytes.count; i++) {
            NSLog(@"%@",propertyBytes[i]);
            NSString *substring = [backContent substringWithRange:NSMakeRange(location, [propertyBytes[i] integerValue])];
            [self setPropertyWithName:propertyNames[i] object:substring];
            
            location += [propertyBytes[i] integerValue];
            
        }
    }
    
    return self;
    
}

- (instancetype)initWithBinaryBackContent:(NSString *)backContent propertyBytes:(NSArray<NSString *> *)propertyBytes{
    
    if ([super init]) {
        NSInteger bytesCount = 0;
        for (NSString *bytes in propertyBytes) {
            
            bytesCount += [bytes integerValue];
            
        }
        
        NSInteger location = 0;
        NSArray *propertyNames = [self propertyOfSelf];

        
        backContent = [NSString hexadecimalToBinary:backContent];

        
        for (NSInteger i = 0; i < propertyBytes.count; i++) {
            
            if (i == 21) {
                
                
                
            }
            
            NSString *substring = [backContent substringWithRange:NSMakeRange(location, [propertyBytes[i] integerValue])];
            [self setPropertyWithName:propertyNames[i] object:substring];
            
            location += [propertyBytes[i] integerValue];
            
        }
    }
    
    return self;
    
}


- (NSInteger)xorOperation{
    
    NSInteger xor = 0;
    NSArray *propertyNames = [self propertyOfSelf];
    for (NSInteger i = 0; i < propertyNames.count; i++) {
        
        NSString *propertyName = propertyNames[i];

        NSString *valueStr = [self sendGetMethodWithPropertyName:propertyName];
        
        
        NSString *decimal = [NSString stringWithFormat:@"%lu",strtoul([[valueStr substringWithRange:NSMakeRange(0, [valueStr length])] UTF8String], 0, 16)];
        
        xor = [decimal integerValue] ^ xor;
        NSLog(@"%@,%ld",decimal,xor);
        
    }
    return xor;

}

- (NSInteger)sumOperation{
    
    NSInteger sum = 0;
    NSArray *propertyNames = [self propertyOfSelf];
    for (NSInteger i = 0; i < propertyNames.count; i++) {
        
        NSString *propertyName = propertyNames[i];
        
        NSString *valueStr = [self sendGetMethodWithPropertyName:propertyName];
        
        
        NSString *decimal = [NSString stringWithFormat:@"%lu",strtoul([[valueStr substringWithRange:NSMakeRange(0, [valueStr length])] UTF8String], 0, 16)];
        
        sum += [decimal integerValue];
        
        
        
    }
    return sum;
    
}



@end
